import AbilityUtil from '@/AbilityUtil';
import BaseUtil from '@/BaseUtil';
import BuffUtil from '@/BuffUtil';
import UnitSpellEvent from '@/event/UnitSpellEvent';
import UnitCommon from 'code/Core/Common/UnitCommon';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `每隔${ColorSafe}10|r秒，如果你下一个技能CD大于2秒，则立刻冷却|n觉醒效果：触发间隔受法术冷却影响`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNEngineeringUpgrade.blp';
const name = '随意修补';
const config: TalentConfigModel = {
    id: 'talent_t_TalentRepireAtWill',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentRepireAtWill',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentRepireAtWill_plus',
        art,
        name: '随意修补[觉醒]',
        ubertip: `每隔${ColorSafe}10|r秒，如果你下一个技能CD大于2秒，则立刻冷却，且该效果受到法术冷却影响`,
    },
};

const effectBuffType: AppBuffType = {
    id: 'buff_t_TalentRepireAtWill_effect',
    art,
    name: '随意修补',
    ubertip: '你的下一个技能如果冷却小于2秒，则在使用后立刻冷却完毕',
};

export default class TalentRepireAtWill extends TalentBase {
    static instance: TalentRepireAtWill;
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentRepireAtWill.instance = this;
        this.setTalentConfig(config);
        this.init();
        se.onUnitSpellEffect(this.spellEffect);
    }
    spellEffect(d: UnitSpellEvent) {
        let u = GetTriggerUnit();
        let _this = TalentRepireAtWill.instance;
        let { unEffect, plus } = _this.talentUnEffect(u);
        if (unEffect) return;
        let buff = BuffUtil.getUnitBuff(u, effectBuffType.id);
        if (buff == null) return;
        let id = GetSpellAbilityId();
        BaseUtil.runLater(0.03, () => {
            let cd = AbilityUtil.getUnitAbilityStateCooldown(u, id);
            if (cd < 2) return;
            AbilityUtil.setUnitAbilityStateCooldown(u, id, 0);
            BuffUtil.clearUnitBuff(u, effectBuffType.id);
            let cool = 10;
            if (plus) {
                let lq = UnitCommon.取冷却降低(u);
                cool *= lq;
            }
            BaseUtil.runLater(cool, () => {
                BuffUtil.addBuff(u, effectBuffType.id, u);
            });
        });
    }
    init() {
        BuffUtil.registerBuffType(effectBuffType);
    }
}
